<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>为cURL传输会话批量设置选项</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.curl-reset.html">curl_reset</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.curl-setopt.html">curl_setopt</a></div>
 <div class="up"><a href="ref.curl.html">cURL 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.curl-setopt-array" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">curl_setopt_array</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.3)</p><p class="refpurpose"><span class="refname">curl_setopt_array</span> &mdash; <span class="dc-title">为cURL传输会话批量设置选项</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.curl-setopt-array-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">bool</span> <span class="methodname"><strong>curl_setopt_array</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$ch</code></span>
   , <span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code></span>
   )</div>

  <p class="para rdfs-comment">
   为cURL传输会话批量设置选项。这个函数对于需要设置大量的cURL选项是非常有用的，不需要重复地调用<span class="function"><a href="function.curl-setopt.html" class="function">curl_setopt()</a></span>。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.curl-setopt-array-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    <dt>
<em><code class="parameter">ch</code></em>
</dt>
<dd>
<p class="para">由 <span class="function"><a href="function.curl-init.html" class="function">curl_init()</a></span> 返回的 cURL 句柄。</p></dd>

    
     <dt>
<em><code class="parameter">options</code></em></dt>

     <dd>

      <p class="para">
       一个<span class="type"><a href="language.types.array.html" class="type array">array</a></span>用来确定将被设置的选项及其值。数组的键值必须是一个有效的<span class="function"><a href="function.curl-setopt.html" class="function">curl_setopt()</a></span>常量或者是它们对等的整数值。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.curl-setopt-array-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果全部的选项都被成功设置，返回<strong><code>TRUE</code></strong>。如果一个选项不能被成功设置，马上返回<strong><code>FALSE</code></strong>，忽略其后的任何在<em><code class="parameter">options</code></em>数组中的选项。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.curl-setopt-array-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4133">
    <p><strong>Example #1 
     初始化一个新的cURL辉煌并抓取一个web页面
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;创建一个新cURL资源<br /></span><span style="color: #0000BB">$ch&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">curl_init</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;设置URL和相应的选项<br /></span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">CURLOPT_URL&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'http://www.example.com/'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">CURLOPT_HEADER&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">false<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">curl_setopt_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;抓取URL并把它传递给浏览器<br /></span><span style="color: #0000BB">curl_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;关闭cURL资源，并且释放系统资源<br /></span><span style="color: #0000BB">curl_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   早于PHP 5.1.3这个函数可以做如下模拟：
  </p>
  <p class="para">
   <div class="example" id="example-4134">
    <p><strong>Example #2 我们对<span class="function"><strong>curl_setopt_array()</strong></span>的等价实现</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">function_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'curl_setopt_array'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">curl_setopt_array</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$ch</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$curl_options</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$curl_options&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$option&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">curl_setopt</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$option</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.curl-setopt-array-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    就<span class="function"><a href="function.curl-setopt.html" class="function">curl_setopt()</a></span>来说，传递一个数组到<strong><code>CURLOPT_POST</code></strong>将会把数据以<em class="emphasis">multipart/form-data</em>的方式编码，然而传递一个URL-encoded字符串将会以<em class="emphasis">application/x-www-form-urlencoded</em>的方式对数据进行编码。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.curl-setopt-array-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.curl-setopt.html" class="function" rel="rdfs-seeAlso">curl_setopt()</a> - 设置一个cURL传输选项</span></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.curl-reset.html">curl_reset</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.curl-setopt.html">curl_setopt</a></div>
 <div class="up"><a href="ref.curl.html">cURL 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
